import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  getters: {
    // getter可以认为是store的计算属性
    // getter正常接受两个参数，分别表示 state, getters（在使用module时，还会有根store的state和getters）
    doubleCount: (state, getters) => {
      console.log('getters函数的参数', state, getters)
      return state.count * 2
    }
  },
  mutations: {
    increment (state) {
      state.count++
    },
    incrementBy (state, amount) {
      state.count += Number(amount)
    },
    asyncAdd (state) {
      state.count += 10
    }
  },
  actions: {
    // 异步操作需要放在actions中执行，然后使用commit交给对应的mutations修改state的值
    asyncAddAction (context) {
      setTimeout (function () {
        context.commit('asyncAdd')
      }, 500)
    }
  }
})

export default store